package com.lindroy.androidplayer.ui.adapter

import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.*
import com.google.android.material.chip.Chip
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.logic.model.KnowledgeItemResp
import com.lindroy.androidplayer.logic.model.KnowledgeResp
import com.lindroy.androidplayer.ui.knowledge.KnowledgeDetailActivity
import com.lindroy.lib_utils.extensions.shortToast

/**
 * @author Lin
 * @date 2021/4/29
 * @function
 */
class KnowledgeIndexAdapter :
    BaseQuickAdapter<KnowledgeResp, BaseViewHolder>(R.layout.knowledge_index_item) {

    override fun convert(holder: BaseViewHolder, item: KnowledgeResp) {
        holder.setText(R.id.tvKnowledgeTitle, item.name)
        val rvKnowledge = holder.getView<RecyclerView>(R.id.rvKnowledge)
        val layoutManager = FlexboxLayoutManager(holder.itemView.context).apply {
            flexWrap = FlexWrap.WRAP
            flexDirection = FlexDirection.ROW
            justifyContent = JustifyContent.FLEX_START
            alignItems = AlignItems.CENTER
        }
        rvKnowledge.layoutManager = layoutManager
        rvKnowledge.adapter= DetailAdapter(item.children,item.name)
    }

    private class DetailAdapter(data:List<KnowledgeItemResp>,val title:String):
        BaseQuickAdapter<KnowledgeItemResp, BaseViewHolder>(R.layout.knowledge_detail_index){
        init{
            setList(data)
        }

        override fun convert(holder: BaseViewHolder, item: KnowledgeItemResp) {
            holder.getView<Chip>(R.id.chipName).apply {
                text = item.name
                setOnClickListener {
                    KnowledgeDetailActivity.start(context,data,title,holder.adapterPosition)
                }
            }
        }

    }
}